package com.xm.settlement.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.xm.settlement.commons.base.BaseController;
import com.xm.settlement.commons.utils.PageInfo;
import com.xm.settlement.model.AppBase;
import com.xm.settlement.service.IAppBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @description：基础管理
        * @author：zhixuan.wang
        * @date：2017/2/23 14:51
        */
@Controller
@RequestMapping("/appBase")
public class AppBaseController extends BaseController {

    @Autowired
    private IAppBaseService appBaseService;

    /**
     * 基础管理主页
     *
     * @return
     */
    @GetMapping(value = "/manager")
    public String manager() {
        return "settlement/appBase/appBase";
    }

    /**
     * 基础管理列表
     *
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo (page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        pageInfo.setCondition(condition);
        appBaseService.selectDataGrid(pageInfo);
        return pageInfo;
    }


    /**
     * 添加基础管理页
     *
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage() {
        return "settlement/appBase/appBaseAdd";
    }

    /**
     * 添加基础管理
     *
     * @param appBase
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(AppBase appBase) {
        appBase.setCreateId(getUserId());
        appBase.setCreateTime(new Date());
        appBaseService.insert(appBase);
        return renderSuccess("添加成功！");
    }


    /**
     * 编辑基础管理页
     *
     * @param model
     * @param id
     * @return
             */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        AppBase appBase = appBaseService.selectById(id);
        model.addAttribute("appBase", appBase);
        return "settlement/appBase/appBaseEdit";
    }


    /**
     * 编辑基础管理
     *
     * @param appBase
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(AppBase appBase) {
        appBaseService.updateSelectiveById(appBase);
        return renderSuccess("编辑成功！");
    }

    /**
     * 删除基础管理
     *
     * @param id
     * @return
     */
        @RequestMapping("/delete")
        @ResponseBody
        public Object delete(Long id) {
            appBaseService.deleteById(id);
            return renderSuccess("删除成功！");
    }
}